package net.chasing.androidbaseconfig.widget.rich;

import net.chasing.androidbaseconfig.widget.rich.base.AbstractRichParser;

import java.util.Locale;

/**
 * 适配 <a></a> 标签的富文本过滤器
 */
public class AtRichParser extends AbstractRichParser {
    private static final String LABEL_CONTENT = "<a target=\"_blank\" href=\"/webshare/userprofile?userid=%d\" data-huixueba-linkuser-id=\"%d\" data-huixueba-linkuser-class=\"symbolat\">@%s</a>";

    private AtRichParser() {
        mLabelPattern = mPattern = "<a[ ]{1}target=\"_blank\"[ ]{1}href=\"/webshare/userprofile[?]userid=[1-9][0-9]{0,}\"[ ]{1}data-huixueba-linkuser-id=\"([1-9][0-9]{0,})\"[ ]{1}data-huixueba-linkuser-class=\"symbolat\">@.{1,}?</a>";
        mRichFormat = " @%s ";
        mIdPattern = "data-huixueba-linkuser-id=\"([1-9][0-9]{0,})\"";
    }

    private static final class MInstanceHolder {
        static final AtRichParser mInstance = new AtRichParser();//单例，全局保持一个解析实例
    }

    public static AtRichParser getInstance() {
        return MInstanceHolder.mInstance;
    }

    /**
     * 将名字及ID转换成对应的<a></a>标签文本
     */
    public String getLabelText(String str, int userId) {
        return String.format(Locale.CHINESE, LABEL_CONTENT, userId, userId, str);
    }
}
